package com.framework.SystemPage
{
	import com.framework.BasicButton;
	import com.framework.BasicPage;
	import com.framework.ButtonInfo;
	import com.framework.CustomEventButton;
	import com.framework.GameDataController;
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	
	
	public class StageModePage extends BasicPage
	{
		
		public var stageBoard_1:resStageBoard1;
		public var stageBoard_2:resStageBoard2;
		public var stageBoard_3:resStageBoard3;
	//	public var btnBack:BasicButton;
		
		public var subpageContainer:Sprite = new Sprite;
		public var subpageSelection_1:StageSelectPage = new StageSelectPage;
		public var subpageSelection_2:StageSelectPage = new StageSelectPage;
		public var subpageSelection_3:StageSelectPage = new StageSelectPage;
		
		public var bg:resBgStageMode = new resBgStageMode;
		
		public var pannel:resStagePannel = new resStagePannel;
		
		public var btnPrev:resBtnPrev = new resBtnPrev;
		public var btnNext:resBtnNext = new resBtnNext;
		
		private var slideDistance:int;
		private var stageNum:int = 3;
		private var currentStageId:int = 0;
		
		public function StageModePage()
		{
			super();
			infoText.text = "StageMode Constructed";
			slideDistance = (bg.width - 550)/(stageNum-1);
			
			
		}
		override public function init(e:Event):void
		{
			super.init(e);
			
			this.addChild(bg);
			
			this.addChild(subpageContainer);
			
		//	btnBack = addButtonByName(ButtonInfo.BUTTON_PLAY,0);
		//	btnBack.txtButtonName.text = "BACK";
			
			
		//	addSwitchButton(btnPrev,20,330);
		//	addSwitchButton(btnNext,480,330);
			
			pannel.prev.addEventListener(MouseEvent.CLICK,BgSliderPrevListener);
			pannel.next.addEventListener(MouseEvent.CLICK,BgSliderNextListener);
			pannel.back.addEventListener(MouseEvent.CLICK,backButtonEventListener);
			pannel.x = 275;
			pannel.y = 365;
			this.addChild(pannel);
			
			
			subpageContainer.addChild(subpageSelection_1);
			subpageContainer.addChild(subpageSelection_2);
			subpageContainer.addChild(subpageSelection_3);
			
			
			subpageSelection_1.createSelections(1,5,GameDataController.getInstance().LevelInfoArray[0]);
			subpageSelection_2.createSelections(2,5,GameDataController.getInstance().LevelInfoArray[1]);
			subpageSelection_3.createSelections(3,5,GameDataController.getInstance().LevelInfoArray[2]);
			subpageSelection_1.createBoard(1,-260,30);
			subpageSelection_2.createBoard(2,-260,30);
			subpageSelection_3.createBoard(3,-260,30);
			
			subpageSelection_1.x = 290;
			subpageSelection_2.x = 290+550*1;
			subpageSelection_3.x = 290+550*2;
			subpageSelection_1.y = 30;
			subpageSelection_2.y = 30;
			subpageSelection_3.y = 30;
		}
		
		override public function update(e:Event):void
		{
			
		}
		
		private function addSwitchButton(btn:MovieClip,val_x,val_y):void
		{
			btn.x = val_x;
			btn.y = val_y;
			btn.buttonMode = true;
			this.addChild(btn);
		
			
		}
		
		
		
		//function slide the page
		private function BgSliderPrevListener(e:MouseEvent):void
		{
			if(currentStageId>0)
			{
				currentStageId--;
			}
			var temp_x = -slideDistance*currentStageId;
			var subpage_x = -550*currentStageId;
			
			TweenLite.to(bg, 1.2, {x:temp_x, ease:Cubic.easeOut});
			TweenLite.to(subpageContainer, 0.8, {x:subpage_x, ease:Sine.easeOut});

		}
		private function BgSliderNextListener(e:MouseEvent):void
		{
			if(currentStageId<stageNum-1)
			{
				currentStageId++;
			}
			var temp_x = -slideDistance*currentStageId;
			var subpage_x = -550*currentStageId;
			
			TweenLite.to(bg, 1.2, {x:temp_x, ease:Cubic.easeOut});
			TweenLite.to(subpageContainer, 0.8, {x:subpage_x, ease:Sine.easeOut});

		}
		
		private function backButtonEventListener(e:MouseEvent):void
		{
			dispatchEvent(new CustomEventButton(CustomEventButton.BUTTON_ID,ButtonInfo.BUTTON_MAIN,0));
			
		}
		

		
	}
}